class Solution
{
public:
    int longestNiceSubarray(vector<int> &nums)
    {
        vector<int> longestNiceEndsWithIndex(nums.size(), 1);
        for (int i = 1; i < nums.size(); ++i)
        {
            int compatibleCount = 0;
            for (int j = 1; j <= min(longestNiceEndsWithIndex[i - 1], i); ++j)
            {
                if ((nums[i] & nums[i - j]) == 0)
                {
                    ++compatibleCount;
                }
                else
                {
                    break;
                }
            }
            longestNiceEndsWithIndex[i] += compatibleCount;
        }
        auto maxIt = max_element(longestNiceEndsWithIndex.begin(), longestNiceEndsWithIndex.end());
        return *maxIt;
    }
};